package com.elitel.frame.base.controller;

import com.elitel.common.pagelist.PageHelper;
import com.elitel.common.core.log.annotation.Log;
import com.elitel.common.core.log.enums.BusinessType;
import com.elitel.frame.base.service.SysOperLogService;
import com.elitel.frame.main.entity.SysOperLog;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author xqh
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController {
    @Autowired
    private SysOperLogService operLogService;

    @GetMapping("/list")
    public Object list(PageInfo page, SysOperLog operLog) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        PageInfo<SysOperLog> pageInfo = new PageInfo<>(list);
        PageResponseVo<SysOperLog> res = new PageResponseVo<>();
        res.setTotal(((Long)pageInfo.getTotal()).intValue());
        res.setRows(pageInfo.getList());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        jsonResult.setMsg("操作成功");
        return jsonResult;
    }


    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operIds}")
    public Object remove(@PathVariable Long[] operIds) {
        operLogService.deleteOperLogByIds(operIds);
        HashMap res = new HashMap<>();
        res.put("code", 200);
        return res;
    }

    @DeleteMapping("/clean")
    public Object clean() {
        operLogService.cleanOperLog();
        HashMap res = new HashMap<>();
        res.put("code", 200);
        return res;
    }
}
